from django.db import models

from utils.BaseModel import BaseModel
from home.models import Nav


# Create your models here.
class Category(BaseModel):
    name = models.CharField(max_length=100, verbose_name="分类名称")
    parent = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE, verbose_name="父级分类",
                               related_name="children")
    intro = models.TextField(verbose_name="分类简介")
    ishome = models.BooleanField(default=False, verbose_name="是否首页展示")

    class Meta:
        db_table = "cate"
        verbose_name = "分类表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Teacher(BaseModel):
    headimg = models.CharField(max_length=200, verbose_name="头像")
    work = models.CharField(max_length=100, verbose_name="职位")
    name = models.CharField(max_length=100, verbose_name="姓名")
    intro = models.TextField(verbose_name="个人简介")

    class Meta:
        db_table = "fgteacher"

    def __str__(self):
        return self.name


class Course(BaseModel):
    title = models.CharField(max_length=100, verbose_name="课程标题")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="课程价格")
    realprice = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="现价")
    students = models.IntegerField(default=0, verbose_name="学习人数")
    level = models.IntegerField(choices=((0, '初级'), (1, '中级'), (2, '高级')), default=0, verbose_name="难度")
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="分类")
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="讲师", related_name="course")
    nav = models.ForeignKey(Nav, on_delete=models.CASCADE, verbose_name="导航")
    intro = models.TextField(verbose_name="课程简介")
    isfinish = models.BooleanField(default=False, verbose_name="是否完结")
    timeline = models.IntegerField(verbose_name="时长")
    score = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="评分")
    detail = models.TextField(verbose_name="课程详情")
    cover = models.CharField(max_length=200, verbose_name="封面")
    vedio = models.CharField(max_length=200, verbose_name="视频")
    isfree = models.BooleanField(default=False, verbose_name="是否免费")

    class Meta:
        db_table = "fgcourse"
        verbose_name = "课程"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Tag(models.Model):
    name = models.CharField(max_length=20, verbose_name="标签名")

    class Meta:
        db_table = "fgtag"
        verbose_name = "标签"

    def __str__(self):
        return self.name


class CourseTag(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE, verbose_name="标签")

    class Meta:
        db_table = "fgcoursetag"
        verbose_name = "课程标签"

    def __str__(self):
        return self.course.title
